<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/5/23 0023
 * Time: 16:07
 */

namespace Api\Controller;


class ShopOrderController extends BaseController
{
    protected $mid;
    protected $member;
    protected $self_shop;
    public function __construct()
    {
        parent::__construct();
        //验证用户
        $openid = I('request.openid','');
        empty($openid) && $this->error('openid缺失！');
        $member = M('Usermember')->where(['openid' => $openid])->find();
        empty($member) && $this->error('用户不存在！');
        $this->mid = $member['id'];
        $this->member = $member;
        // 查询商家信息
        $self_shop = M('Shop')->where(['user_id' => $this->mid,'status' => '2'])->find();
        empty($self_shop) && $this->error('您不是商家或未通过审核！');
        $this->self_shop = $self_shop;
    }

    /**
     * 查询被投放广告列表
     */
    public function index()
    {
        $order_list = M('AdOrderList')->where(['shop_id' => $this->self_shop['id']])->select();
        $order = M('AdOrder')->where(['order_no' => array('in',array_unique(array_column($order_list,'order_no')))])->select();
        foreach ($order as &$item) {
            $item['list'] = [];
            foreach ($order_list as $value) if($value['order_no'] === $item['order_no']) {
                $value['qrcode_path'] = SITE_URL . $value['qrcode_path'];
                $item['list'][] = $value;
            }
        }
        $this->success('获取被投放广告列表成功！',$order);
    }
    /**
     * 查询被投广告详情
     * @param $id3
     */
    public function read($id)
    {
        $order_list = M('AdOrderList')->where(['shop_id' => $this->self_shop['id']])->select();
        if(!in_array($id,array_unique(array_column($order_list,'order_no')))){
            $this->error('查询订单信息失败！');
        }
        $order = M('AdOrder')->where(['order_no' => $id])->find();
        empty($order) && $this->error('查询订单信息失败！');
        $order['list'] = [];
        foreach ($order_list as $item) if($item['order_no'] === $order['order_no']) {
            $item['qrcode_path'] = SITE_URL . $item['qrcode_path'];
            $order['list'][] = $item;
        }
        $this->success('获取订单详情成功！',$order);
    }

}